package com.bdcom.autotest.telnet.util;

import com.bdcom.autotest.telnet.constant.TelnetConstant;
import com.bdcom.autotest.telnet.model.Result;

public class TelnetUtil implements TelnetConstant {
    /**
     * 根据返回字符串判断下发命令是否执行成功
     * @param retStr
     * @return
     */
    public static Result executeStatus(String retStr, String cmd){
        Result result=new Result();
        String errMessage="";
        retStr=retStr.toLowerCase();
        if (retStr.contains(UNKNOW_COMMAND.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+UNKNOW_COMMAND;
            result.setFailedReason(errMessage);
            result.setStatus(false);
            return result;
        }
        if (retStr.contains(WRITE_TO_FILE_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+WRITE_TO_FILE_ERROR;
            result.setFailedReason(errMessage);
            result.setStatus(false);
            return result;
        }
        if (retStr.contains(PARAMETER_INVALID.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+PARAMETER_INVALID;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(INCOMPLETE_COMMAND.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+INCOMPLETE_COMMAND;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if(retStr.contains(TOO_MANY_PARAM.toLowerCase()))
        {
            errMessage="cmd:"+"'"+cmd+"' "+TOO_MANY_PARAM;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(TIMEOUT.toLowerCase())||retStr.contains(Timed_Out.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+TIMEOUT;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(SUCCEED.toLowerCase())&&!retStr.contains(WRITE_TO_FILE_ERROR.toLowerCase())) {
            result.setStatus(true);
            return result;
        }
        if (retStr.contains(SERVER_IP_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+SERVER_IP_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(ERROR_ENABLE.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+ERROR_ENABLE;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }

        if (retStr.contains(MALLOC_FAIL.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+MALLOC_FAIL;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }

        if (retStr.contains(FILESYS_BUSY.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+FILESYS_BUSY;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(UNABLE_GET_FILE_SIZE.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+UNABLE_GET_FILE_SIZE;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(MEMORY_ALLOC_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+MEMORY_ALLOC_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(FREE_SPACE_NOT_ENOUGH.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+FREE_SPACE_NOT_ENOUGH;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(ENTER_FILESYS_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+ENTER_FILESYS_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(OPEN_FILE_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+OPEN_FILE_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(WRITE_FILE_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+WRITE_FILE_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(FILE_CLOSE_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+FILE_CLOSE_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(COPY_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+COPY_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(SYS_MEY_ERROR.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+SYS_MEY_ERROR;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(COPY_ERROR_NOT_WRITE_FLASH.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+COPY_ERROR_NOT_WRITE_FLASH;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(ACCESS_DENIED.toLowerCase())) {
            errMessage="enable password error";
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(ERROR_FROM_SERVER.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+ERROR_FROM_SERVER;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(USER.toLowerCase())) {
            errMessage="This device maybe third party device!";
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        if (retStr.contains(FILE_NOT_FOUND.toLowerCase())) {
            errMessage="cmd:"+"'"+cmd+"' "+FILE_NOT_FOUND;
            result.setStatus(false);
            result.setFailedReason(errMessage);
            return result;
        }
        errMessage = retStr;
        result.setStatus(false);
        result.setFailedReason(errMessage);
        return result;
    }

    public static Result executeStatusForFormatReboot(String retStr, String cmd){
        Result result=new Result();
        String errMessage="";
        retStr=retStr.toLowerCase();
        if (retStr.contains(FORMAT_SUCCEED_FLAG.toLowerCase())) {
            //执行format命令的成功
            result.setStatus(true);
            return result;
        }
        if (retStr.contains(MONITOR_FLAG.toLowerCase())) {
            //reboot成功后进入monitor模式
            result.setStatus(true);
            return result;
        }

        errMessage = retStr;
        result.setStatus(false);
        result.setFailedReason(errMessage);
        return result;


    }

    public static Result executeStatusForReboot(String retStr){
        Result result=new Result();
        String errMessage="";
        retStr=retStr.toLowerCase();
        if (retStr.contains(MASTER_UP_FLAG.toLowerCase())) {
            //reboot成功后进入master模式
            result.setStatus(true);
            result.setRetMsg("Master");
            return result;
        }
        if (retStr.endsWith(MONITOR_FLAG.toLowerCase())) {
            //reboot成功后进入monitor模式
            result.setStatus(true);
            result.setRetMsg("Monitor");
            return result;
        }

        errMessage = retStr;
        result.setStatus(false);
        result.setFailedReason(errMessage);
        return result;


    }


}
